<?php
	class Validation 
	{
		/* Check wether the password is of atleast 8 characters length and contains atleast 3 of the following:
		*  Small letters.
		*  Capital letters.
		*  Numbers.
		*  Special letters.
		*/
		public function ValidatePassword($password) 
		{
			if (preg_match("/\s+/", $password)) 
			{
				return false;
			}

			if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password)) 
			{
				return true;
			} 
			else 
			{
				return false;
			}
		}
		
		// Strip away all tags except for the ones given as a parameter to strip_tags.
		public function StripSome($parameter) 
		{
			$parameter = strip_tags($parameter, '<br><b><u><i><h2>');
	
			return $parameter;
		}
		
		// Strips away all tags, no exceptions. # WARNING # May or may not protect against inline javascript.
		public function StripAll($parameter) 
		{
			$parameter = strip_tags($parameter);
	
			return $parameter;
		}
	
		// Returns wether the given email was valid or not.
		public function ValidateEmail($email) 
		{
			$regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,7}$/";
	
			if (preg_match($regexp, $email)) 
			{
				return true;
			} 
			else 
			{
				return false;
			}
		}
		
		// Validates wether the given parameter is of the type int.
		public function ValidateNumber($number) 
		{
			if (is_int($number) == True) 
			{
				return true;
			}
			return false;
		}
	
		// Checks wether the date given was valid or not.
		public function ValidateDate($date) 
		{
			$date = $this -> numericFromString($date);
	
			if (strlen($date) == 8) 
			{
				$date = substr($date, -6);
			}
	
			if (strlen($date) == 6) 
			{
				$y = substr($date, 0, -4);
				$d1 = substr($date, -2);
				$m = substr($date, 2, -2);
	
				if (checkdate($m, $d1, $y) == TRUE) 
				{
					return true;
	
				} 
				else 
				{
					return false;
				}
			}
		}
	
		// Checks wether the given PersonalIdentificationNumber was valid or not.
		public function ValidateSSN($parameter) 
		{
			$ssn = $this -> numericFromString($parameter);
	
			if (strlen($ssn) == 12) 
			{
				$ssn = substr($ssn, -10);
			}
	
			if (strlen($ssn) == 10) 
			{
				if (substr($ssn, 2, -6) > 12) 
				{
					return false;
				}
	
				if (substr($ssn, 4, -4) > 31) 
				{
					return false;
				}
	
				if ($this -> luhn($ssn) == TRUE) 
				{
	
					return true;
				} 
				else 
				{
					return false;
				}
			} 
			else 
			{
				return false;
			}
		}
	
		// Returns any numbers from a string.
		function numericFromString($string) 
		{
			return preg_replace('/[^0-9]/', '', $string);
		}
	
		// Runs the luhn algorithm on the given parameter.
		function luhn($ssn) 
		{
			$sum = 0;
	
			for ($i = 0; $i < strlen($ssn) - 1; $i++) 
			{
				$tmp = substr($ssn, $i, 1) * (2 - ($i & 1));
				//växla mellan 212121212
				if ($tmp > 9)
					$tmp -= 9;
				$sum += $tmp;
			}
	
			//extrahera en-talet
			$sum = (10 - ($sum % 10)) % 10;
	
			return substr($ssn, -1, 1) == $sum;
		}
		
		// Returns true if the input was empty and false if it was not.
		public function IsInputEmpty($parameter)
		{
			if ($parameter != null || $parameter != "")
			{
				return false;
			}			
			return true;
		}
	}
